﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace HundSunDBQryFrmClient.Frm
{
    public partial class FormLogin : Form
    {
        public WebRsp value;
        public FormLogin()
        {
            InitializeComponent();
            textBox1.Text = ConfigurationManager.AppSettings["UserID"]??"";
            textBox2.Text= ConfigurationManager.AppSettings["Password"] ?? "";
            if (textBox1.Text!="")
            {
                textBox2.Focus();
                //textBox1.Focus();
            }
            textBox2.PasswordChar = '*';
            checkBox1.Checked=bool.Parse(ConfigurationManager.AppSettings["SaveUserID"]??"true");
            this.FormBorderStyle=FormBorderStyle.FixedSingle;
            this.AcceptButton = button1;
            this.CancelButton = button2;
        }

        public static WebRsp show() 
        {
            FormLogin frm=new FormLogin();
            frm.ShowDialog();
            return frm.value;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //登录
            string UserID=textBox1.Text;
            string Password=textBox2.Text;
            string ServerBaseUrl= ConfigurationManager.AppSettings["ServerBaseUrl"];
            if (string.IsNullOrEmpty(ServerBaseUrl))
            {
                MessageBox.Show("ServerBaseUrl未设置");
                return;
            }
            if (string.IsNullOrEmpty(UserID)|| string.IsNullOrEmpty(Password))
            {
                MessageBox.Show("用户名或密码不能为空");
                return;
            }
            using (HttpClient client=new HttpClient())
            {
                try
                {
                    string url = new Uri(new Uri(ServerBaseUrl), "/api/UserLogin").ToString();
                    StringContent content = new StringContent($"UserID={UserID}&Password={Password}");
                    var response = client.PostAsync(url, content).Result;
                    string resstr = response.Content.ReadAsStringAsync().Result;
                    var webrsp = JsonConvert.DeserializeObject<WebRsp>(resstr);
                    if (webrsp != null)
                    {
                        if (webrsp.code == 0)
                        {
                            value = webrsp;
                            if (checkBox1.Checked)
                            {
                                Program.UpdateSetting("UserID", UserID);
                                Program.UpdateSetting("Password", Password);
                            }
                            Close();
                        }
                        else
                        {
                            MessageBox.Show(webrsp.errmsg);
                        }
                    }
                    else
                    {
                        MessageBox.Show("服务器返回错误");
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("服务器连接错误");
                }
                
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            Program.UpdateSetting("SaveUserID", checkBox1.Checked.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
